<?php

class Subject extends AppModel{
    var $name='Subject';
    var $useTable='subject';

    var $validate=array(
        'subject'=>array(
            'noEmpty'=>array(
            'rule'=>'notEmpty',
            'message'=>'Fill in subject name',
                'on' => 'create'
                ),
            'unique'=>array(
                'rule'=>'unique',
                'message'=>'Subject Already added.',
                'on' => 'create'
            )
        )
    );
    var $hasMany=array('Attribute'=>array(
        'className'=>'Attribute',
        'foreignKey'=>'subject_id'
        ),
            'SubjectRate'=>array(
        'className'=>'SubjectRate',    
                'foreignKey'=>'subject_id',
                'order'=>'SubjectRate.created DESC',
                'group'=>'SubjectRate.user_id'
            ),
        'Subjectredirect'=>array(
            'className'=>'Subjectredirects',
            'foreignKey'=>'subject_id'
            
        ),
        'SubjectAdjectives'=>array(
            'className'=>'SubjectAdjectives',
            'foreignKey'=>'subject_id'
        ),
        'Comment'=>array(
            'className'=>'Comment',
            'foreignkey'=>'subject_id'
        )
                );
    
    var $belongsTo=array('User'=>array(
        'className' => 'User',
        'foreignKey' => 'user_id')
    );
    
      function isAllowed(){
        if($this->data['Subject']['image']['error']=='0'){
        $allowed=True;
                
        $allowed_ext=array('image/jpeg','image/jpg','image/png','image/gif','image/JPEG','image/JPG','image/PNG','image/GIF');
        foreach($allowed_ext as $extension){
            if($extension==$this->data['Subject']['image']['type']){
                $allowed=True;
             }
        }
        return $allowed;
            
        }else{
            return true;
        }
        
    }
        
     function findCSV($modelname,$field,$ids=null){
         if(!empty($id)){
             $id_array=explode(',',$id);
             foreach($id_array as $id){
                 $data=$this->$model->find('first',array('conditions'=>array('$model.id ='=>$id)));
                 //$this->data[]=$adjective.','.$data[]
             }
         
     }
     
     }
     function notZero(){
         //debug($this->data);die();
         if($this->data['Subject']['tag_ids']=='') return false;
         else return true;
     }
     
     function unique(){
         if(isset($this->data['Subject']['subject'])){
            
             $data=$this->find('first',array('conditions'=>array('Subject.subject'=>$this->data['Subject']['subject'],'Subject.desc'=>$this->data['Subject']['desc'])));
             if($data){
                 return false;
             }else{
                 return true;
             }
         }else{
             return false;
         }
     }
     
     function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    
}
    
?>